package com.Web.Servlet;

import com.Web.Person.Teacher;
import com.Web.Service.ITeacherService;
import com.Web.Service.Impl.TeacherServiceImpl;
import com.Web.util.JDBCUtil;
import com.Web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {

    private ITeacherService teacherService = new TeacherServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null || method.equals("")){
            method = "selectByPage";
        }
        switch (method) {
            case "selectAll":
                selectAll(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "selectTeacherAddPage":
                selectTeacherAddPage(req,resp);
                break;
            case "toUpDate":
                toUpDate(req,resp);
                break;
            case "selectByPage":
                selectByPage(req,resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String address = req.getParameter("address");
        Teacher teacher = new Teacher(Integer.parseInt(id), name, Integer.parseInt(age), address);
        teacherService.update(teacher);
        resp.sendRedirect("/teacher");
    }

    private void toUpDate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TeacherServlet.toUpDate");
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        Teacher teacher = teacherService.toUpDate(id);
        req.setAttribute("teacher", teacher);
        req.getRequestDispatcher("/teacher_update.jsp").forward(req,resp);
    }

    private void selectTeacherAddPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TeacherServlet.selectTeacherAddPage");
        req.getRequestDispatcher("/teacher_add.jsp").forward(req,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println(".add");
        String name = req.getParameter("name");
        String ages = req.getParameter("age");
        int age = Integer.parseInt(ages);
        String address = req.getParameter("address");
        Teacher teacher = new Teacher(name, age, address);
        teacherService.add(teacher);
        resp.sendRedirect("/teacher");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.deleteById");
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);
        teacherService.deleteById(i);
        resp.sendRedirect("/teacher");
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TeacherServlet.selectAll");
        List<Teacher> list = teacherService.selectAll();
        req.setAttribute("list", list);
        req.getRequestDispatcher("teacher_list.jsp").forward(req, resp);
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TeacherServlet.selectById");
        String pageNo = req.getParameter("pageNo");
        if(pageNo == null || pageNo.equals("")){
            pageNo = "1";
        }
        String pageSize = req.getParameter("pageSize");
        if(pageSize == null || pageSize.equals("")){
            pageSize = "5";
        }
        PageInfo<Teacher> pageInfo = teacherService.selectByPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        req.setAttribute("pageInfo", pageInfo);
        req.getRequestDispatcher("teacher_list.jsp").forward(req, resp);
    }
}
